package utils;

import org.apache.commons.lang.StringUtils;

import play.cache.Cache;

/**
 * 语音播报线程
 * @author wangshaoming
 *
 */
public class BroadThread implements Runnable{
	private String name;
	private String path;
	private String content;
	private String tone;
	private String speed;

	public BroadThread(String name,String path, String content, String tone, String speed) {
		super();
		this.name=name;
		this.path = path;
		this.content = content;
		this.tone = tone;
		this.speed = speed;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		String lastCont=content;
		int i=1;
		while(true){
			//大于200个字符的时候截取200，其他的时候直接截取200
			if(content.length()>BizConstants.CUTCONTENTLENGTH){
				lastCont=content.substring(0, BizConstants.CUTCONTENTLENGTH);
				//获取最后一个标点位置，如果标点位置不存在则按照截取限定值截取
				int lastPunc=BroadUtils.getLastPunc(lastCont);
				if(lastPunc<=0){
					lastPunc=BizConstants.CUTCONTENTLENGTH;
				}
				lastCont=content.substring(0, lastPunc);
			}
			else{
				lastCont=content;
			}
			String fileName=this.name+"_"+i;
			String vidpath=BroadUtils.postBroad(lastCont, tone, speed,fileName);
			if(StringUtils.isEmpty(vidpath)){
				continue;
			}
			//将本次合成语音名称放入缓存
			String files=(String)Cache.get(this.name);
			files+=";"+Integer.toString(i);
			Cache.set(this.name, files,"12h");
			i++;
			content=content.substring(lastCont.length(), content.length());
			if(content.length()<=0){
				break;
			}
		}
	}
	
}
